﻿$(function () {
    $('.btn.btn-primary').click(function () {
        try {
            if (this.classList.contains('save')) {
                var view_mode = $('#ViewMode').html(),
                    acc_id = $('#acc_id').html(),
                    row,
                    post_url = "/user",
                    post_data = {
                        _csrf: $("meta[name=x-csrf-token]").attr("content"),
                        view_mode: view_mode
                    };
                if (pre_submit_validate()) {
                    switch (view_mode) {
                        case "EditPsw":
                            post_url += "/edit_psw";
                            row = {
                                _id: acc_id,
                                old_psw: $('#old_psw').val(),
                                password: $('#password').val()
                            };
                            post_data.data = JSON.stringify(row);
                            break;
                        case "EditInfo":
                            post_url += "/edit_userinfo";
                            row = {
                                _id: acc_id,
                                emp_name: $("#emp_name").val(),
                                emp_no: $("#emp_no").val(),
                                dpt_name: $("#dpt_name").val()[0],
                                position: $("#position").val()[0]
                            };
                            post_data.data = JSON.stringify(row);
                            break;
                    }
                    $.post(post_url, post_data, function (data) {
                        $('.form-group').children('span').remove();
                        data = JSON.parse(data);
                        if (data.code != 200) { showModal("错误信息", data.data); }
                        else { showModal("保存成功"); }
                    }).error(function (jxr, status, msg) {
                        showModal("系统错误", "保存失败!\r\n错误信息:<" + jxr.status + ">" + msg);
                    });
                } else {
                    showModal("提示", "请按要求完成表格！");
                }
            }
            if (this.classList.contains('exit')) {
                window.document.location.href = "/user/";
            }
        } catch (e) {
            showModal("内部错误", e.message + "\r\n\r\n" + e.stack);
        }
    })
})